package ext.tianma.part.AVLMaterial.queue;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import wt.fc.PersistenceHelper;
import wt.fc.QueryResult;
import wt.method.RemoteAccess;
import wt.method.RemoteMethodServer;
import wt.query.QuerySpec;
import wt.query.SearchCondition;
import wt.queue.ProcessingQueue;
import wt.queue.QueueEntry;
import wt.queue.QueueHelper;
import wt.session.SessionHelper;
import wt.util.WTException;
import ext.tianma.model.APLQuantity;
import ext.tianma.part.AVLMaterial.service.AVLMaterialService;

public class AddEntry implements RemoteAccess {
	private static final Logger logger = LoggerFactory.getLogger(AddEntry.class);

	public static void main(String[] args) throws WTException {
		searchAdvanceCon();
	}

	/**
	 * 添加条目
	 * 
	 * @param queueName  队列名称
	 * @param methodName 方法名称
	 * @param className  类名
	 * @param argsType
	 * @param args
	 * @return
	 * @throws WTException
	 */
	public static boolean addEntry(String queueName, String methodName, String className, Class[] argsType,
			Object[] args) throws WTException {

		if (!RemoteMethodServer.ServerFlag) {
			try {
				Class[] argType = { String.class, String.class, Class[].class, Object[].class };
				Object[] arg = { methodName, className, argsType, args };
				RemoteMethodServer.getDefault().invoke("addEntry", AddEntry.class.getName(), null, argsType, args);
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {
			String user = "";
			try {
				user = SessionHelper.manager.getPrincipal().getName();
				SessionHelper.manager.setAdministrator(); // 设置权限
				ProcessingQueue queue = getScheduleQueue(queueName);
				QueueEntry entry = queue.addEntry(SessionHelper.manager.getAdministrator(), methodName, className,
						argsType, args);
				if (entry != null) {
					return true;
				}
			} finally {
				if (StringUtils.isNotBlank(user)) {
					SessionHelper.manager.setPrincipal(user);
				}
			}
		}
		return false;

	}

	/**
	 * 根据队列名称查询指定队列
	 * 
	 * @param queueName
	 * @return
	 * @throws WTException
	 */
	public static ProcessingQueue getScheduleQueue(String queueName) throws WTException {
		ProcessingQueue queue = QueueHelper.manager.getQueue(queueName);
		String user = "";
		try {
			user = SessionHelper.manager.getPrincipal().getName();
			SessionHelper.manager.setAdministrator();

			if (queue == null) {
				queue = QueueHelper.manager.createQueue(queueName);
			}
			if (!queue.isEnabled()) {
				QueueHelper.manager.enableQueue(queue, true);
				QueueHelper.manager.startQueue(queue);
			}
		} finally {
			if (StringUtils.isNotBlank(user)) {
				SessionHelper.manager.setPrincipal(user);
			}
		}
		return queue;
	}
	
	/**
	 * 条目运行方法
	 * @author xucr
	 * @CreateDate 2020年11月13日
	 */
	public static void run1() {
		try {
			List<APLQuantity> qs = findAdvances();
			AVLMaterialService.getRangeChangeData(qs);
		} catch (WTException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 检索进阶数据
	 * 
	 * @return
	 * @throws WTException
	 */
	public static QueryResult searchAdvanceCon() throws WTException {
		QuerySpec qs = new QuerySpec(APLQuantity.class);
		qs.setAdvancedQueryEnabled(true);
		SearchCondition sc1 = new SearchCondition(APLQuantity.class, APLQuantity.PROMOTION, SearchCondition.IS_TRUE, true);
		qs.appendWhere(sc1, new int[] { 0 });
		qs.appendAnd();
		SearchCondition sc2 = new SearchCondition(APLQuantity.class, APLQuantity.PROCESSES, SearchCondition.IS_FALSE, false);
		qs.appendWhere(sc2, new int[] { 0 });
		QueryResult qr = PersistenceHelper.manager.find(qs);
		logger.info("qs>>>" + qs.toString());

		return qr;
	}
	
	/**
	 * 需要进阶的数据
	 * @return
	 * @author xucr
	 * @throws WTException 
	 * @CreateDate 2020年11月13日
	 */
	public static List<APLQuantity> findAdvances() throws WTException {
		List<APLQuantity> qs = new ArrayList<APLQuantity>();
		QueryResult qr = searchAdvanceCon();
		while(qr.hasMoreElements()) {
			APLQuantity q = (APLQuantity) qr.nextElement();
			qs.add(q);
		}
		return qs;
	}
}
